﻿using Common.HttpApis;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GptChatBatch.WinUI.Forms
{
    public partial class FrmChatGpt : Form
    {
        public FrmChatGpt()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            
            string input= txtInput.Text.Trim();
            string userId= txtUserId.Text.Trim();
            if (string.IsNullOrEmpty(input)) { 
                MessageBox.Show("请输入内容");
                return;
            }
            else
            {
                rtxtOutput.AppendText("问："+input+"\r\n");
                lstbHistory.Items.Add(input);
                btnSend.Enabled = false;
            }
            Task.Run(() => {
                ChatGptHttp chatGptHttp= new ChatGptHttp { prompt= input,userId= userId };
                //string data = "{\"prompt\":\""+ input + "\",\"userId\":\"#/chat/1685099056344\",\"network\":true,\"system\":\"\",\"withoutContext\":false,\"stream\":false}"; ;
                string data=JsonConvert.SerializeObject(chatGptHttp);
                this.Invoke(new Action(() =>
                {
                    lblTip.Text = "正在思考中...";
                    rtxtOutput.AppendText("答：");
                }));                               
                try
                {
                    //string output = ChatGptHttp.Post(data);
                    //this.Invoke(new Action(() =>
                    //{
                    //    btnSend.Enabled = true;
                    //    rtxtOutput.AppendText("答：" + output + "\r\n");
                    //    lblTip.Text = "请提问";
                    //}));
                    //
                    ChatGptHttp.Post(data, (character) => {
                        this.Invoke(new Action(() =>
                        {
                            if (character != -1)
                            {
                                rtxtOutput.AppendText(((char)character).ToString());
                            }
                            else
                            {
                                rtxtOutput.AppendText("\r\n");
                                btnSend.Enabled = true;
                                lblTip.Text = "请提问";
                            }
                            rtxtOutput.ScrollToCaret();
                        }));

                    });
                    
                }
                catch (Exception err)
                {
                    this.Invoke(new Action(() =>
                    {
                        btnSend.Enabled = true;
                        rtxtOutput.AppendText("答：" + "出错了" + "\r\n");
                        rtxtOutput.ScrollToCaret();
                        lblTip.Text = "请提问";
                    }));

                }


            });
            
        }

        private void FrmChatGpt_Load(object sender, EventArgs e)
        {
            string[] history = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory,"CfgFiles/question_list.txt"));
            lstbHistory.Items.AddRange(history);
            txtInput.Focus();
        }

        private void lstbHistory_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            txtInput.Text=lstbHistory.Text;
            txtInput.Focus();
        }
    }
}
